import http from "@/utils/http";
/**
 * 佳博打印机打印方法
 *
 * @param {string} mrlCode 物料编号
 * @param {string} mrlName 物料名称
 * @param {string} lotCode 批次号
 * @param {int} qty 数量
 * @param {string} barCode 单号
 * @param {int} tagNum 标签数量
 */
function printToJiaBo(
  mrlCode,
  mrlName,
  lotCode,
  qty,
  barCode,
  tagNum,
  callback
) {
  let uuid = localStorage.getItem("uuid");
  if (!uuid) {
    window.api.alert({
      title: "打印失败",
      msg: "在打印机设置中连接打印机后，可恢复打印条码功能！"
    });
  }
  let printParams = {
    mac: uuid,
    code: mrlCode,
    name: mrlName,
    lot: lotCode,
    num: qty,
    con: barCode,
    tagnum: 1
  };
  if (mrlCode != "测试打印连接") {
    //保存到localStorage
    localStorage.setItem("backUpPrintParams", JSON.stringify(printParams));
  }

  let privacy = window.api.require("ble");
  let printModule = window.api.require("moduleDemo");
  // privacy.initManager(function(ret) {
  //   if (ret.state == "poweredOn") {
  printModule.printData2(printParams, function(ret) {
    window.api.toast({ msg: ret.result, location: "middle" });
  });
  // if (callback) {
  //   callback();
  // }
  // } else {
  //   window.api.alert({
  //     title: "打印失败",
  //     msg: "请开启蓝牙！"
  //   });
  // }
  // });
}

function reTag() {
  let backUpPrintParams = localStorage.getItem("backUpPrintParams");
  let printObj = JSON.parse(backUpPrintParams);
  console.log(JSON.stringify(printObj));

  let printModule = window.api.require("moduleDemo");
  printModule.printConnect(printObj, function(ret) {
    window.api.toast({ msg: ret.result, location: "middle" });

    printModule.printData(printObj, function(ret) {
      window.api.toast({ msg: ret.result, location: "middle" });
    });
  });
}

export { printToJiaBo, reTag };
